﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Collections {
	public class PagedList<T> : List<T>, IPagedList {

		public PagedList(IQueryable<T> source, int index, int pageSize) {
			int num = source.Count<T>();
			this.TotalCount = num;
			this.TotalPages = num/pageSize;
			if ((num%pageSize) > 0)
				this.TotalPages++;
			this.PageSize = pageSize;
			this.PageIndex = index;
			base.AddRange(source.Skip<T>((index*pageSize)).Take<T>(pageSize).ToList<T>());
		}

		public PagedList(IEnumerable<T> source, int totalRecords, int index, int pageSize) {
			this.TotalCount = totalRecords;
			this.TotalPages = totalRecords/pageSize;
			if ((totalRecords%pageSize) > 0)
				this.TotalPages++;
			this.PageSize = pageSize;
			this.PageIndex = index;
			base.AddRange(source.ToList<T>());
		}

		public bool IsNextPage {
			get { return ((this.PageIndex*this.PageSize) <= this.TotalCount); }
		}

		public bool IsPreviousPage {
			get { return (this.PageIndex > 0); }
		}

		public int PageIndex { get; set; }

		public int PageSize { get; set; }

		public int TotalCount { get; set; }

		public int TotalPages { get; set; }
	}
}